package so.shanku.essential.activity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import aym.util.getservicesdata.GetServicesDataQueue;
import aym.util.getservicesdata.GetServicesDataUtil;
import aym.util.json.JsonMap;
import aym.util.json.JsonParseHelper;
import net.tsz.afinal.annotation.view.ViewInject;
import so.shanku.essential.R;
import so.shanku.essential.activity.base.BaseUIActivity;
import so.shanku.essential.app.MyApplication;
import so.shanku.essential.getdata.GetDataConfing;
import so.shanku.essential.getdata.GetDataQueue;
import so.shanku.essential.getdata.JsonKeys;
import so.shanku.essential.getdata.ShowGetDataError;
import so.shanku.essential.utils.ExtraKeys;
import so.shanku.essential.utils.Utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * Created by Jaelyn on 2015/9/10 0010.
 * 关注品牌
 */
public class AttentionActivity extends BaseUIActivity implements View.OnClickListener {
	@ViewInject(id = R.id.attention_layout)
	private LinearLayout attention_layout;

	@ViewInject(id = R.id.go_shopping_bt, click = "go_shopping_bt_click")
	private Button go_shopping_bt;
	private TextView loadMoreTv;
	private int pageSize = 10;//每次显示的行数
	private int currentPage = 0;
	private LinearLayout.LayoutParams layoutParams;
	private boolean isHasFooter = false;
	public static final String[] keywords = {"QQ", "BaseAnimation", "APK",
			"GFW", "铅笔",//
			"短信", "桌面精灵", "MacBook Pro", "平板电脑", "雅诗兰黛",//
			"Base", "笔记本", "SPY Mouse", "Thinkpad E40", "捕鱼达人",//
			"内存清理", "地图", "导航", "闹钟", "主题",//
			"通讯录", "播放器", "CSDN leak", "安全", "Animation",//
			"美女", "天气", "4743G", "戴尔", "联想",//
			"欧朋", "浏览器", "愤怒的小鸟", "mmShow", "网易公开课",//
			"iciba", "油水关系", "网游App", "互联网", "365日历",//
			"脸部识别", "Chrome", "Safari", "中国版Siri", "苹果",//
			"iPhone5S", "摩托 ME525", "魅族 MX3", "小米"};
	public List<JsonMap<String, Object>> dataAttention;
	private String position;
	private int flag = 1;//0 app启动正常进入的   1：已经启动，再次进入登录

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.attention_brand_layout);
		try {
			flag = getIntent().getIntExtra(ExtraKeys.Key_Msg1, 0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (flag == 0) {
			//setBtn_back(R.drawable.back_delete);
		} else {

		}
		setCenter_title(R.string.attention_brand_title);
		layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		getData_UserLogin();
	}
	int startIndex = 0;
	private void showBrands(int page) {

		for (int lineNums=0; lineNums < pageSize;lineNums++ ) {
			Random r = new Random();
			int randomNum = r.nextInt(2) + 2;

			LinearLayout linearLayout = new LinearLayout(this);
			linearLayout.setGravity(Gravity.CENTER_HORIZONTAL);
			int j;
			for ( j= startIndex; j < startIndex + randomNum; j++) {
				if (j >= dataAttention.size()) {
					break;
				}
				LinearLayout attention_layout1 = (LinearLayout) getLayoutInflater().inflate(R.layout.attention_brand_item, null);
				TextView textView = (TextView) attention_layout1.findViewById(R.id.attention_name_tv);
				textView.setText(dataAttention.get(j).getStringNoNull("Name"));
				attention_layout1.setTag(j);
				attention_layout1.setOnClickListener(this);
				startAnimation(attention_layout1);
				linearLayout.addView(attention_layout1);
			}
			attention_layout.addView(linearLayout, layoutParams);
			startIndex=j;

			if (j >= dataAttention.size()) {
				break;
			}
		}

		if (startIndex < dataAttention.size()) {
			LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
			layoutParams1.gravity = Gravity.CENTER_HORIZONTAL;
			if (isHasFooter) {
				attention_layout.removeView(loadMoreTv);
			} else {
				loadMoreTv = (TextView) getLayoutInflater().inflate(R.layout.simple_tv, null);
				loadMoreTv.setTag(-1);
				loadMoreTv.setTextSize(18);
				loadMoreTv.setText(R.string.more);
				loadMoreTv.setCompoundDrawablePadding(getResources().getDimensionPixelSize(R.dimen.default_drawpadding));
				loadMoreTv.setOnClickListener(this);
				loadMoreTv.setGravity(Gravity.CENTER_HORIZONTAL);
				Drawable drawable = getResources().getDrawable(R.drawable.version_checking);
/// 这一步必须要做,否则不会显示.
				drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
				loadMoreTv.setCompoundDrawables(null, null, drawable, null);
			}
			isHasFooter = true;
			attention_layout.addView(loadMoreTv, layoutParams1);

		} else {
			if (isHasFooter) {
				attention_layout.removeView(loadMoreTv);
			}
			//toast.showToast(R.string.load_over);
		}

		currentPage++;
	}

	private View v;

	@Override
	public void onClick(View v) {
		this.v = v;
		Integer tag = (Integer) v.getTag();
		if (tag == -1) {
			showBrands(currentPage);
		} else {
			position = dataAttention.get(tag).getStringNoNull("Id");
			getDate_GetFocusOn(callBack, position, this);

		}
	}

	public void go_shopping_bt_click(View view) {
//		if (flag == 0) {
//			if (MyApplication.getInstance().getAttentionBrands().size() == 0) {
//				toast.showToast(R.string.no_attention);
//			} else {
//				jumpToAndClearAll(MainActivity.class);
//			}
//		} else {
//			jumpToAndClearAll(MainActivity.class);
//		}
//		jumpToAndClearAll(MainActivity.class);
		finish();
	}

	/**
	 * 关注品牌去购物
	 */
	private void getData_UserLogin() {
		loadingToast.show();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(GetDataQueue.Params_key, GetDataConfing.Key_Str);
		params.put("UserInfoId", Utils.getUserId(this));
		GetDataQueue queue = new GetDataQueue();
		queue.setCallBack(callBack);
		queue.setActionName(GetDataConfing.GetAction_GetVendorList);
		queue.setWhat(GetDataConfing.what_GetVendorList);
		queue.setParamsNoJson(params);
		getDataUtil.getData(queue);
	}


	private void startAnimation(LinearLayout attention_layout1) {
		Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha_in);
		anim.setFillAfter(true);
		attention_layout1.startAnimation(anim);
	}

	private void startAnimation1() {
		Animation anim = AnimationUtils.loadAnimation(this, R.anim.slide_up_in);
		anim.setFillAfter(true);
		attention_layout.startAnimation(anim);
	}
	/**
	 * 获取服务器数据的返回信息
	 */
	private GetServicesDataUtil.IGetServicesDataCallBack callBack = new GetServicesDataUtil.IGetServicesDataCallBack() {

		@Override
		public void onLoaded(GetServicesDataQueue entity) {
			if (entity.isOk()) {
				if (ShowGetDataError.isOkAndCodeIsNot1(mContext,entity.getInfo())) {
					if (entity.what == GetDataConfing.what_GetVendorList) {
						List<JsonMap<String, Object>> data = JsonParseHelper.getJsonMap_List_JsonMap(
								entity.getInfo(), JsonKeys.Key_Info);
						dataAttention = data;
						showBrands(currentPage);
						startAnimation1();
					} else if (entity.what == GetDataConfing.WHAT_GET_FOCUS_ON) {
						List<JsonMap<String, Object>> data = JsonParseHelper.getJsonMap_List_JsonMap(
								entity.getInfo(), JsonKeys.Key_Info);
						try {
							boolean isFocus = data.get(0).getBoolean("IsFocus", false);
							String vendorId = data.get(0).getStringNoNull("VendorId");
							if (isFocus) {
								v.setSelected(true);
								MyApplication.getInstance().addAttendBrands(vendorId);
							} else {
								v.setSelected(false);
								MyApplication.getInstance().removeAttendBrands(vendorId);
							}
						} catch (Exception e) {
							e.printStackTrace();
						}


					}
				}
			} else {
				ShowGetDataError.showNetError(mContext);
			}
			loadingToast.dismiss();
		}
	};

	@Override
	public void finish() {
//		if (flag == 0) {
//			if (MyApplication.getInstance().getAttentionBrands().size() == 0) {
//				toast.showToast(R.string.no_attention);
//			} else {
//				super.finish();
//			}
//		} else {
//			super.finish();
//		}
		jumpToAndClearAll(MainActivity.class);
	}
}